<?php


namespace app\admin1\controller;


use app\admin1\model\GoodsModel;
use app\admin1\validate\GoodsValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class GoodsController extends AdminBaseController
{
    //页面显示
    public function index(){
        $price = input('price','');
//        halt($price);
        $where = [];
        if ($price){
            $where[] = ['price', 'like', "%$price%"];
        }
        $list = GoodsModel::order(['list_order' => 'DESC'])
            ->where($where)
            ->paginate(4);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加页面显示
    public function add(){
        return $this->fetch();
    }
    //添加方法
    public function addPost(){
        $data = input('post.','');
        $data['photos_url'] = implode(',', $data['photos_url']);
        $validate = new GoodsValidate();
        $res = $validate->scene('add')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }

//        $images = $data['images'];
//        $urls = cmf_get_image_url($images);
//        $image = $data['image'];
//        $url = cmf_get_image_url($image);
        if(GoodsModel::create($data)){
            $this->success('添加成功','index');
        }else{
            $this->error('添加失败');
        }
    }
    //修改页面显示
    public function edit(){
        $id = input('id');
        $goods = GoodsModel::find($id);
        $this->assign('goods',$goods);
        return $this->fetch();
    }
    //修改方法
    public function editPost(){
        $data = input('post.');
        $data['photos_url'] = implode(',', $data['photos_url']);
        //验证器
        $validate = new GoodsValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        //查询是否有此记录
        $list = GoodsModel::find($data['id']);
        if(!$list){
            $this->error('未查询到该记录');
        }
        if($list->update($data)){
            $this->success('修改成功','Goods/index');
        }else{
            $this->error('修改失败');
        }
    }
    //删除
    public function delete(){
        $id = input('id/d','');
        if (!$id){
            $this->error('参数错误');
        }
        if (GoodsModel::destroy($id)){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
    public function order(){
        parent::listOrders(new GoodsModel());
        $this->success('排序成功');
    }
    //营销形式切换开关
    public function change(){
        $id = input('id');
        if(!$id){
            $this->error('参数错误');
        }
        $new = GoodsModel::find($id);
        if($new['type'] == 0){
            $newType = 1;
        }else{
            $newType = 0;
        }
        $new->type = $newType;
        if($new->save()){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
    //打开配置界面
   public function setGoodsAttr(){
       $goodsId = input('goods_id');
       $this->assign('goods_id',$goodsId);
       return $this->fetch('add_goods_attr_2');
   }
    ///配置商品属性提交
    public function setGoodsAttrPost()
    {
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
        if(count($attrs) < 1 ){
            $this->error('请至少配置一个属性! ');
        }
        Db::name('goods_attr')
            ->where('goods_id',$goodsId)
            ->delete();
        $insert_data = [];
        foreach ($attrs as $attr) {
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr['name']
            ];
        }
        Db::name('goods_attr')->insertAll($insert_data);
        $this->success('保存成功');
    }
    //打开配置商品属性值页面
    public function setGoodsAttrValue(){
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('add_goods_attr_value');
    }
    //提交表单
    public function setGoodsAttrValuePost(){
        $data = input('post.');
//        halt($data['form']);
        if (count($data)<1){
            return json(['code'=>0,'msg'=>'请输入一个属性值','data'=>null]);
        }
        $insert_data =[];
        foreach ($data['form'] as $attrs){
            $insert_data[] =[
                'attr_id'=>$attrs['attr'],
                'name'=>$attrs['attr_value']
            ];
        }
        $addValues = Db::name('goods_attr_value')->insertAll($insert_data);
        if ($addValues) {
            $this->success('添加成功');
        }

    }
    //    查询某个商品有哪些属性值
    public function getGoodsAttr(){
        $goodsId = input('goods_id');
        //查询商品的属性值
        $attrs = Db::name('goods_attr')
            ->where('goods_id',$goodsId)
            ->select();
        return json(['code' => 1,'msg' => '商品属性','data' => $attrs]);
    }
}